package com.mingyizhudao.app.view.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dixiang.framework.utils.Utils;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.common.IDataLoadListenterByFlg;
import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.home.bean.DisNavsEntity;
import com.mingyizhudao.app.view.search.adpter.SearchAdapter;
import com.mingyizhudao.app.view.search.bean.SearchEntity;

public class SearchActivity extends MYBaseActivity{
	private ArrayList<DisNavsEntity> disNavs;
	private TextView tv_search;
	private EditText et_search;
	private Map<String,SearchEntity> map = new HashMap<String,SearchEntity>();
	private SearchAdapter adapter;
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		initView();
	}

	@Override
	public void initHeaderView() {
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void initView(){
		disNavs = (ArrayList<DisNavsEntity>) getIntent().getSerializableExtra("disNavs");
		listView = (ListView) findViewById(R.id.list_search);
		et_search = (EditText) findViewById(R.id.et_search);
		tv_search = (TextView)findViewById(R.id.tv_search);
		tv_search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				et_search.setText("");
				adapter.setData(new SearchEntity());
				adapter.notifyDataSetChanged();
			}
		});
		
		
		
		et_search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				String key = arg0+"";
				if(Utils.isStringEmpty(arg0)) return;
				doSeach(key);
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
			}
		});
		
		adapter = new SearchAdapter(this,listView,disNavs,getIntent().getStringExtra(Constants.PARM_ACTION_URL));
	}
	
	private void doSeach(String key){
		SearchEntity result = map.get(key);
		
		if(result == null){
			String path = DataUtils.getInitial().doSearch(activity, key, new HashMap<String,Object>(), listenter);	
		}else{
			doChangeView(key);
		}
		
	}
	
	private IDataLoadListenterByFlg listenter = new IDataLoadListenterByFlg() {
		
		@Override
		public void onSuccess(Object result, String flg) {
			if(result != null){
				SearchEntity entity = (SearchEntity)result;
				map.put(flg, entity);
				doChangeView(flg);
			}else{
				map.put(flg, null);
			}
		}
		
		@Override
		public void onError(Exception e, Object result, String flg) {
			map.put(flg, null);
		}
		
		@Override
		public void onCompleted(Exception e, Object result, String flg) {
			
		}
	};
	
	private void doChangeView(String flg){
		String value = et_search.getText().toString(); 
		if(value.equals(flg)){
			SearchEntity entity = map.get(flg);
			adapter.setData(entity);
			listView.setAdapter(adapter);
		}
	}
	
//	private void initView() {

//		disNavs = (ArrayList<DisNavsEntity>) getIntent().getSerializableExtra("disNavs");
//		
//		
//		et_search = (EditText)findViewById(R.id.et_search);
//		tv_search = (TextView)findViewById(R.id.tv_search);
//
//		selectList = new ArrayList<CityEntity>();
//		CityEntity cityEntity0 = new CityEntity(0,"找医生");
//		CityEntity cityEntity1 = new CityEntity(1,"找科室");
//		selectList.add(cityEntity0);
//		selectList.add(cityEntity1);


//		tv_search.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//
//				input = et_search.getText().toString().trim();
//				if (input==null||(input!=null&&input.length()==0)) {
//					Toast.makeText(activity, "请输入疾病名称！", Toast.LENGTH_SHORT).show();
//				}else {
//					doNet();
//				}
//			}
//		});
//	}


//	private void getDeptAlert() {
//		final AlertDialog dlg = new AlertDialog.Builder(this).create();
//		dlg.show();
//		Window window = dlg.getWindow();
//		// *** 主要就是在这里实现这种效果的.
//		// 设置窗口的内容页面,dialog_fb_instruction.xml文件中定义view内容
//		// window.setWindowAnimations(R.style.mydlgstyle);  //添加动画
//		window.setContentView(R.layout.dlg_dept);
//		// window.setGravity(Gravity.BOTTOM);
//
//		dlg.setCanceledOnTouchOutside(true);
//		LinearLayout  ll_title= (LinearLayout) window.findViewById(R.id.ll_title);
//		ListView lv_dept = (ListView) window.findViewById(R.id.lv_dept);
//		ll_title.setVisibility(View.GONE);
//
//		CityListAdapter cityListAdapter = new CityListAdapter(activity, selectList,"CENTER");
//		lv_dept.setAdapter(cityListAdapter);
//		lv_dept.setOnItemClickListener(new OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
//				dlg.cancel();
//				if (position==0) {
//					
//					Bundle bundle = new Bundle();
//					bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) disNavs);
//					bundle.putString(Constants.PARM_ACTION_URL, getIntent().getStringExtra(Constants.PARM_ACTION_URL));
//					bundle.putString("diseaseId", diseaseId);
//					IntentHelper.getInstance(activity).gotoActivity(SearchDoctorActivity.class, bundle);
//					
//				}else if (position==1) {
//					Bundle bundle = new Bundle();
//					bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) disNavs);
//					bundle.putString("diseaseId", diseaseId);
//					IntentHelper.getInstance(activity).gotoActivity(SearchDeptActivity.class, bundle);
//				}
//			}
//
//		});
//	}
	
}
